﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.OleDb;

namespace DAO
{
    public class CTDHDAO
    {
       
        public int kiemtrathuoc(int maDH, int maSP)
        {
            string sql = " SELECT COUNT(*) FROM CTDatHang WHERE MaDH = " + maDH + " AND MaSP = " + maSP;
            OleDbConnection conn = DataProvider.ConnectDB();
            OleDbCommand cmd = new OleDbCommand(sql, conn);
            object obj = cmd.ExecuteScalar();
            int kq = obj == DBNull.Value ? 0 : Convert.ToInt32(obj);
            conn.Close();
            return kq;
        }

        public List<CTDatHangDTO> LayDSCTDatHang(int maDH)
        {
            string sql = "SELECT * FROM CTDatHang WHERE MaDH = " + maDH;
            OleDbConnection conn = DataProvider.ConnectDB();
            OleDbCommand cmd = new OleDbCommand(sql, conn);
            OleDbDataReader reader = cmd.ExecuteReader();
            List<CTDatHangDTO> danhsach = new List<CTDatHangDTO>();

            while (reader.Read())
            {
                CTDatHangDTO ctDTO = new CTDatHangDTO();
                ctDTO.MaDH = reader.GetInt32(0);
                ctDTO.MaSP = reader.GetInt32(1);
                ctDTO.NgayDat = reader.GetDateTime(2);
                ctDTO.SoLuong = reader.GetInt32(3);
                ctDTO.DonGia = reader.GetDouble(4);
                ctDTO.ThanhTien = reader.GetDouble(5);
                ctDTO.Diem = reader.GetInt32(6);
                ctDTO.ConLai = reader.GetInt32(7);
                danhsach.Add(ctDTO);
            }
            conn.Close();
            return danhsach;
        }

        public void CapNhatSoLuongConLai(int maDH, int maSP , int SL)
        {
            string sql = " UPDATE CTDatHang SET ConLai = ConLai + " + SL + " WHERE MaDH = " + maDH + " AND MaSP = " + maSP;
            OleDbConnection conn = DataProvider.ConnectDB();
            OleDbCommand cmd = new OleDbCommand(sql, conn);
            cmd.ExecuteNonQuery();
        }

        public int LaySoLuongConLai(int maDH, int maSP)
        {
            string sql = " SELECT ConLai FROM CTDatHang WHERE MaDH = " + maDH + " AND MaSP = " + maSP;
            OleDbConnection conn = DataProvider.ConnectDB();
            OleDbCommand cmd = new OleDbCommand(sql, conn);
            object obj = cmd.ExecuteScalar();
            int kq = obj == DBNull.Value ? 0 : Convert.ToInt32(obj);
            conn.Close();
            return kq;

        }
        
    }
}
